<?php

namespace app\canteen\admin;

use app\common\builder\ZBuilder;
use app\common\model\StDept as StDeptModel;

class StDept extends Base
{

    public function index()
    {
        $map = $this->getMap();
        if (UID > 1) $map =['comid'=>COMID,'delete_time'=>['>',0]];
        $data_list = StDeptModel::where($map)->order('sort,id')->column(true, 'id');
        $max_level = $this->request->get('max', 0);
        $this->assign('menus', $this->getNestMenu($data_list, $max_level));
       return $this->fetch();
    }

    //新增
    public function add($parent_code='0')
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'StDept');
            if(true !== $result) $this->error($result);
            $data['parent_code']= $parent_code;
            //dump($data);exit();
            if (StDeptModel::create($data)) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','comid',COMID],
                ['text', 'code', '部门编号'],
                ['text', 'dept_name', '部门名称'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->layout(['code'=>7,'dept_name'=>7,'parent_code'=>7,'sort'=>7])
            ->fetch();
    }

    //编辑
    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'StDept');
            if(true !== $result) $this->error($result);
            if (StDeptModel::update($data)) {
                $this->success('编辑成功', 'index');
            } else {
                $this->error('编辑失败');
            }
        }

        $info = StDeptModel::get($id);
        // 显示编辑页面
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['text', 'parent_code', '父级编号'],
                ['text', 'code', '部门编号'],
                ['text', 'dept_name', '部门名称'],
                ['number', 'sort', '排序'],
                ['radio', 'status', '立即启用', '', ['否', '是'], 1]
            ])
            ->setFormdata($info)
            ->layout(['code'=>7,'dept_name'=>7,'parent_code'=>7,'sort'=>7])
            ->fetch();
    }



    /**
     * 获取嵌套式节点
     * @param array $lists 原始节点数组
     * @param int $pid 父级id
     * @param int $max_level 最多返回多少层，0为不限制
     * @param int $curr_level 当前层数
     * @author 陸月 <360164460@qq.com>
     * @return string
     */
    private function getNestMenu($lists = [], $max_level = 0, $pid = "0", $curr_level = 1)
    {
        $result = '';
        foreach ($lists as $key => $value) {
            if ($value['parent_code'] == "$pid") {
                $disable  = $value['status'] == 0 ? 'dd-disable' : '';

                // 组合节点
                $result .= '<li class="dd-item dd3-item '.$disable.'" data-id="'.$value['id'].'">';
                $result .= '<div class=" dd3-handle">拖拽</div><div class="dd3-content"><i class="'.$value['icon'].'"></i> '.$value['dept_name'].' ('.$value['code'].')';
                $result .= '<div class="action">';
                $result .= '<a href="'.url('add', ['parent_code' => $value['code']]).'" data-toggle="tooltip" data-original-title="新增子节点"><i class="list-icon fa fa-plus fa-fw"></i></a><a href="'.url('edit', ['id' => $value['id']]).'" data-toggle="tooltip" data-original-title="编辑"><i class="list-icon fa fa-pencil fa-fw"></i></a>';
                if ($value['status'] == 0) {
                    // 启用
                    $result .= '<a href="javascript:void(0);" data-ids="'.$value['id'].'" class="enable" data-toggle="tooltip" data-original-title="启用"><i class="list-icon fa fa-check-circle-o fa-fw"></i></a>';
                } else {
                    // 禁用
                    $result .= '<a href="javascript:void(0);" data-ids="'.$value['id'].'" class="disable" data-toggle="tooltip" data-original-title="禁用"><i class="list-icon fa fa-ban fa-fw"></i></a>';
                }
                $result .= '<a href="'.url('delete', ['id' => $value['id'], 'table' => 'admin_menu']).'" data-toggle="tooltip" data-original-title="删除" class="ajax-get confirm"><i class="list-icon fa fa-times fa-fw"></i></a></div>';
                $result .= '</div>';

                if ($max_level == 0 || $curr_level != $max_level) {
                    unset($lists[$key]);
                    // 下级节点
                    $children = $this->getNestMenu($lists, $max_level, $value['code'], $curr_level + 1);
                    if ($children != '') {
                        $result .= '<ol class="dd-list">'.$children.'</ol>';
                    }
                }

                $result .= '</li>';
            }
        }
        return $result;
    }

}